package com.aier.school.action;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.common.SchoolUtil;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolGradeAttendance;
import com.aier.school.service.ISchoolService;

@Scope("prototype")
@Component("schoolGradeManageAction")
public class SchoolGradeManageAction extends SchoolBaseAction{

    /**
     * 学校管理-职务管理
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private Integer sid;        //学校id
    private List<SchoolGrade> schoolGradeList;    //职务列表
    private SchoolGrade schoolGrade;            //职务
    private Long gid;            //职务id
    private String gname;        //职务名
    private String gdesinfo;    //职务说明
    private String daynum;        //作息周期(多个以,分隔)
    private String txt_sadate;    // 上午开始时间 格式为HH:mm
    private String txt_eadate;    // 上午结束时间
    private String txt_spdate;    // 下午开始时间
    private String txt_epdate;    // 下午结束时间
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
    @Override
    public String execute(){
        return SUCCESS;
    }    
    
    @SuppressWarnings("unchecked")
    public String searchSchoolGradeList(){
        s = 1;    
        Map<String,Object> map = schoolService.searchSchoolGradeList(page, SchoolFinalPageSize.TEACHER_TYPE_LIST_PAGESIZE, sid);
        if(null==map){
            s = 0;
        }
        schoolGradeList = (List<SchoolGrade>) map.get(ICommonDAO.OBJ_LIST);
        if (null == schoolGradeList) {
            s = 0;
        }else{
            schoolGradeList = doforDateStr(schoolGradeList);
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    
    /**
     * APP端查询职务列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSchoolGradeListApp(){
        s = 1;    
        Map<String,Object> map = schoolService.searchSchoolGradeList(page, page_size, sid);
        if(null==map){
            s = 0;
        }
        schoolGradeList = (List<SchoolGrade>) map.get(ICommonDAO.OBJ_LIST);
        if (null == schoolGradeList) {
            s = 0;
        }else{
            schoolGradeList = doforDateStr(schoolGradeList);
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 获取要修改的职务
     */
    public String showSchoolGrade(){
        schoolGrade = schoolService.searchSchoolGradeByCondition(gid);
        return SUCCESS;
    }
    
    /**
     * 删除职务
     * @return
     */
    public String deleteSchoolGrade(){
        s = schoolService.deleteSchoolGrade(gid);
        return SUCCESS;
    }
    
    /**
     * 添加职务
     */
    public String addSchoolGrade(){
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm");
        Time sadate = null, eadate = null, spdate = null, epdate = null;
        try {
            if (!"".equals(txt_sadate.trim())) {
                sadate = new Time(df2.parse(txt_sadate).getTime());
            }
            if (!"".equals(txt_eadate.trim())) {
                eadate = new Time(df2.parse(txt_eadate).getTime());
            }
            if (!"".equals(txt_spdate.trim())) {
                spdate = new Time(df2.parse(txt_spdate).getTime());
            }
            if (!"".equals(txt_epdate.trim())) {
                epdate = new Time(df2.parse(txt_epdate).getTime());
            }
            if(schoolService.checkSchoolGradeByName(gname,sid,null)!=0){
                s = 0;
                error_info = "职务名已经存在";
            }else{
                String[] agdates = daynum.split(",");
                s = schoolService.insertSchoolGrade(sid, gname, gdesinfo, agdates, sadate, eadate, spdate, epdate);
            }            
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }    
        return SUCCESS;
    }
    
    /**
     * 修改职务
     */
    public String updateSchoolGrade(){
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm");
        Time sadate = null, eadate = null, spdate = null, epdate = null;
        try {
            if (!"".equals(txt_sadate.trim())) {
                sadate = new Time(df2.parse(txt_sadate).getTime());
            }
            if (!"".equals(txt_eadate.trim())) {
                eadate = new Time(df2.parse(txt_eadate).getTime());
            }
            if (!"".equals(txt_spdate.trim())) {
                spdate = new Time(df2.parse(txt_spdate).getTime());
            }
            if (!"".equals(txt_epdate.trim())) {
                epdate = new Time(df2.parse(txt_epdate).getTime());
            }
            if(schoolService.checkSchoolGradeByName(gname,sid,gid)>0){
                s = 0;
                error_info = "职务名已经存在";
            }else{
                String[] agdates = daynum.split(",");
                s = schoolService.updateSchoolGrade(gid, gname, gdesinfo, agdates, sadate, eadate, spdate, epdate);
            }                       
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }  

        return SUCCESS;
    }


    
    /**
     * 设置职务作息时间显示
     * @param sgList2
     * @return
     */
    public List<SchoolGrade> doforDateStr(List<SchoolGrade> sgList2) {
        String tmp_date1 = "",tmp_date2 = "";    
        for(SchoolGrade scGrage:sgList2){
            List<SchoolGradeAttendance> sgAttendanceList = schoolService.searchSchoolGradeAttendanceList(scGrage.getGid());
            if(sgAttendanceList!=null&&sgAttendanceList.size()>0){
                //1星期日2星期一3星期二4星期三5星期四6星期五7星期六
                boolean flag = true;
                SchoolGradeAttendance sga = sgAttendanceList.get(0);
                List<Integer> tmpList = new ArrayList<Integer>();
                tmpList.add(sga.getType().intValue());
                tmp_date1 = SchoolUtil.showTimeStr(sga);
                for(int i=1;i<sgAttendanceList.size();i++){
                    SchoolGradeAttendance sga1 = sgAttendanceList.get(i);
                    tmpList.add(sga1.getType().intValue());
                    tmp_date2 = SchoolUtil.showTimeStr(sga1);
                    if(tmp_date1.equals(tmp_date2)){
                        continue;
                    }else{
                        scGrage.setDate_str("作息时间不固定");
                        flag = false;
                        break;
                    }    
                }
                if(flag){
                    scGrage.setDate_str(SchoolUtil.dateShow(tmpList)+" "+tmp_date1);
                }
            }else{
                scGrage.setDate_str("未设置作息时间");
            }
        }
        return sgList2;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public SchoolGrade getSchoolGrade() {
        return schoolGrade;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public void setGdesinfo(String gdesinfo) {
        this.gdesinfo = gdesinfo;
    }

    public void setDaynum(String daynum) {
        this.daynum = daynum;
    }

    public void setTxt_sadate(String txt_sadate) {
        this.txt_sadate = txt_sadate;
    }

    public void setTxt_eadate(String txt_eadate) {
        this.txt_eadate = txt_eadate;
    }

    public void setTxt_spdate(String txt_spdate) {
        this.txt_spdate = txt_spdate;
    }

    public void setTxt_epdate(String txt_epdate) {
        this.txt_epdate = txt_epdate;
    }

    public Integer getSid() {
        return sid;
    }

}
